<template>
  <h1>我是SEI</h1>
</template>

<script setup lang="ts">
import SuperTable from '@/framework/components/core/super-table/super-table.vue';
import DmEditModal from '@/modules/platform-management/pages/device-management/components/dm-edit-modal.vue';
import { VxeGridPropTypes } from 'vxe-table';
import type { TableColumnItem } from './columns';
import { baseColumns } from './columns';
import { reactive, ref } from 'vue';
import Button from '@/framework/components/basic/button/button.vue';
import { PlusOutlined } from '@ant-design/icons-vue';
import { deviceQuery, deviceDetail } from '@/api/base/device-data';
import { searchDevice } from '@/api/emq-openapi';

/**
 * data
 */
const state = reactive({
  currentTitle: '',
  showModal: false,
  currentData: {},
});

/**
 * fn
 */
const showModalFn = (params: string, info?: any): void => {
  if (params === '新增') {
    state.currentData = info ?? {};
  }
  state.currentTitle = params;
  state.showModal = true;
};

/**
 * table
 */
const superTableRef = ref<InstanceType<typeof SuperTable>>();
const columns: TableColumnItem[] = [
  ...baseColumns,
  {
    title: '操作',
    field: '$action',
    hideInSearch: true,
    align: 'center',
    fixed: 'right',
    width: 150,
    actions: ({ row }) => [
      {
        label: '注册',
        ifShow: row.status === 0,
        onClick: () => {
          showModalFn('新增', row);
        },
      },
      {
        label: '查找',
        ifShow: !!row.online,
        onClick: () => {},
      },
      {
        label: '管理',
        onClick: async () => {
          let res = await deviceDetail(row.deviceId);
          state.currentData = res.result;
          showModalFn('修改');
        },
      },
    ],
  },
];
const fetchData = async (ori: VxeGridPropTypes.ProxyAjaxQueryParams) => {
  let data = superTableRef.value?.getFormModel();
  let params = {
    pageSize: 1000000,
    pageNum: ori.page.currentPage,
    orderBy: [
      {
        field: 'createTime',
        order: 'desc',
      },
    ],
    ...data,
  };
  let res = await deviceQuery(params);
  let tempList: any[] = [];
  let allRegistedDevices = new Set();
  res.result.data.forEach((item) => {
    allRegistedDevices.add(item.sn);
    tempList.push({
      deviceId: item.id,
      sn: item.sn,
      name: item.name,
      type: item.type,
      createTime: item.cteateTime,
      status: 1,
    });
  });
  // 向emq查询设备在线状态并主动发现设备
  let sub = await searchDevice({ conn_state: 'connected' });
  let newData: any[] = [];
  let onlineSet = new Set();
  sub.data.forEach((item) => {
    // 主动的发现所有在线的平板、定位仪、交接仓
    if (item.connected) {
      if (item.clientid.startsWith('PAD') || item.clientid.startsWith('LOC') || item.clientid.startsWith('BOX')) {
        let infos = item.clientid.split('-');
        let type = infos[0];
        let sn = infos[1];
        onlineSet.add(sn);
        if (!allRegistedDevices.has(sn)) {
          newData.push({
            type,
            sn,
            name: '未分配名称',
            status: 0,
            online: true,
          });
        }
      }
    }
  });
  tempList = tempList.map((item: any) => ({
    ...item,
    online: onlineSet.has(item.sn),
  }));
  if (!data.type && !data.name && !data.sn) {
    tempList = [...newData, ...tempList];
  }
  return {
    data: tempList,
    pageIndex: 1,
    pageSize: 1000000,
    total: 1000000,
  };
};
const refreshTableFn = () => {
  superTableRef.value?.refreshTable();
};
</script>

<style scoped lang="less"></style>
